package cn.chenhongchao.zhaobiaopingtai.service.impl;

import cn.chenhongchao.zhaobiaopingtai.common.api.CommonPage;
import cn.chenhongchao.zhaobiaopingtai.constant.BidConstant;
import cn.chenhongchao.zhaobiaopingtai.dto.ReturnBidDTO;
import cn.chenhongchao.zhaobiaopingtai.entity.*;
import cn.chenhongchao.zhaobiaopingtai.mapper.BidMapper;
import cn.chenhongchao.zhaobiaopingtai.service.BidService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BidServiceImpl extends ServiceImpl<BidMapper, Bid> implements BidService {


    @Autowired
    private TenderServiceImpl tenderService;
    @Override
    public CommonPage<ReturnBid> dynamicQuery(Integer pageNum, Integer pageSize, ReturnBidDTO returnBidDTO) {

        QueryWrapper<Bid> wrapper = new QueryWrapper<>();
//        wrapper.eq("state", -1);
        // TODO 暂时查全部  需要判断哪个用户 进行 过滤 获取当前访问的用户
        // TODO  wrapper.eq("userId", "userId");
//        // 分装返回类型
//        list.stream().map(tender -> {
//
//        });

        // 分页查询
        Page<Bid> page = new Page<>(pageNum, pageSize);
        baseMapper.selectPage(page, wrapper);
        // 分装返回需要的类型
        CommonPage<ReturnBid> returnBidCommonPage = new CommonPage<ReturnBid>();

        List<Bid> records = page.getRecords();
        List<ReturnBid> collect = records.stream().map(bid -> {
            Integer tenderid = bid.getTenderid();
            Tender tender = tenderService.getById(tenderid);

            ReturnBid returnBid = new ReturnBid();
            returnBid.setTenderName(tender.getTendername());
            returnBid.setId(bid.getBidid());
            returnBid.setTenderNumber(bid.getTenderid());
            returnBid.setTenderFileStatus("!lost");
            returnBid.setTenderScheduleStatus(BidConstant.招标文件以发布);

            // openBidDTO.setTenderScheduleStatus();
            return returnBid;
        }).collect(Collectors.toList());
        returnBidCommonPage.setList(collect);


        Long pages = page.getPages();
        Integer total = Integer.valueOf(pages.toString());
        returnBidCommonPage.setTotalPage(total);

        returnBidCommonPage.setPageSize(pageSize);

//        Long current = page.getCurrent();
//        Integer num = Integer.valueOf(current.toString());
        returnBidCommonPage.setPageNum(pageNum);
        returnBidCommonPage.setTotal(page.getTotal());


        return returnBidCommonPage;
    }

    @Override
    public CommonPage<ReturnBid> getTenderEvaluation(Integer pageNum, Integer pageSize, ReturnBidDTO returnBidDTO) {
        // 只要未审核的
        QueryWrapper<Bid> wrapper = new QueryWrapper<Bid>().eq("state", 0);
        // 分页查询
        Page<Bid> page = new Page<>(pageNum, pageSize);
        baseMapper.selectPage(page, wrapper);

        // 分装返回需要的类型
        CommonPage<ReturnBid> returnBidCommonPage = new CommonPage<ReturnBid>();

        List<Bid> records = page.getRecords();
        List<ReturnBid> collect = records.stream().map(bid -> {
            Integer tenderid = bid.getTenderid();
            Tender tender = tenderService.getById(tenderid);

            ReturnBid returnBid = new ReturnBid();
            returnBid.setTenderName(tender.getTendername());
            returnBid.setId(bid.getBidid());
            returnBid.setTenderNumber(bid.getTenderid());
            returnBid.setTenderFileStatus("!lost");

            if (tender.getTenderType().equals("技术标")) {
                returnBid.setTenderScheduleStatus(BidConstant.待评技术标);
            } else if (tender.getTenderType().equals("商务标")){
                returnBid.setTenderScheduleStatus(BidConstant.待评商务标);
            }

            // TODO 先设置初始值
            returnBid.setCommName("a");
            returnBid.setContractDemand("b");


            return returnBid;
        }).filter(item -> {
            Boolean isjishubiao = returnBidDTO.getIsjishubiao();
            if (isjishubiao) {
                return item.getTenderScheduleStatus().equals(BidConstant.待评技术标);
            } else {
                return item.getTenderScheduleStatus().equals(BidConstant.待评商务标);
            }
        }).collect(Collectors.toList());
        returnBidCommonPage.setList(collect);
        Long pages = page.getPages();
        Integer total = Integer.valueOf(pages.toString());
        returnBidCommonPage.setTotalPage(total);
        returnBidCommonPage.setPageSize(pageSize);

        returnBidCommonPage.setPageNum(pageNum);
        returnBidCommonPage.setTotal(page.getTotal());
        return returnBidCommonPage;
    }

    @Override
    public CommonPage<WinBidApproval> getWinByTender(Integer pageNum, Integer pageSize, ReturnBidDTO returnBidDTO) {

        // 只要未审核的
        QueryWrapper<Bid> wrapper = new QueryWrapper<Bid>().eq("state", 3);
        // 分页查询
        Page<Bid> page = new Page<>(pageNum, pageSize);
        baseMapper.selectPage(page, wrapper);

        // 分装返回需要的类型
        CommonPage<WinBidApproval> returnBidCommonPage = new CommonPage<>();

        List<Bid> records = page.getRecords();
        List<WinBidApproval> collect = records.stream().map(bid -> {
            Integer tenderid = bid.getTenderid();
            Tender tender = tenderService.getById(tenderid);

            WinBidApproval winBidApproval = new WinBidApproval();
            winBidApproval.setTenderName(tender.getTendername());
            winBidApproval.setId(bid.getBidid());
            winBidApproval.setTenderNumber(bid.getTenderid());
            winBidApproval.setTenderFileStatus("!lost");
//            winBidApproval.setTenderScheduleStatus(BidConstant.中标审定中);
            winBidApproval.setTenderScheduleStatus(bid.getTenderScheduleStatus());
//            System.out.println("==============================================");
//            System.out.println("===========bid.getTenderScheduleStatus()=================" + bid.getTenderScheduleStatus());
//            System.out.println("==============================================");
            winBidApproval.setTenderType(bid.getTenderType());

            //TODO 不知道什么意思
            winBidApproval.setCommName("a");
            winBidApproval.setContractDemand("b");


            return winBidApproval;
        }).collect(Collectors.toList());
        returnBidCommonPage.setList(collect);

        Long pages = page.getPages();
        Integer total = Integer.valueOf(pages.toString());
        returnBidCommonPage.setTotalPage(total);
        returnBidCommonPage.setPageSize(pageSize);

        returnBidCommonPage.setPageNum(pageNum);
        returnBidCommonPage.setTotal(page.getTotal());
        return returnBidCommonPage;
    }

    @Override
    public CommonPage<BidReport> getTenderByCompiledReport(Integer pageNum, Integer pageSize, ReturnBidDTO returnBidDTO) {

        QueryWrapper<Bid> wrapper = new QueryWrapper<Bid>().eq("state", 2)
                .or().eq("state", 1)
                .or().eq("state", 3);
        // 分页查询
        Page<Bid> page = new Page<>(pageNum, pageSize);
        baseMapper.selectPage(page, wrapper);

        // 分装返回需要的类型
        CommonPage<BidReport> returnBidCommonPage = new CommonPage<>();

        List<Bid> records = page.getRecords();
        List<BidReport> collect = records.stream().map(bid -> {
            Integer tenderid = bid.getTenderid();
            Tender tender = tenderService.getById(tenderid);

            BidReport bidReport = new BidReport();
            bidReport.setTenderName(tender.getTendername());
            bidReport.setId(bid.getBidid());
            bidReport.setTenderNumber(bid.getTenderid());
            bidReport.setTenderFileStatus("!lost");
            bidReport.setTenderScheduleStatus(BidConstant.待编制综合评标报告);

            //TODO 不知道什么意思
            bidReport.setCommName("a");
            bidReport.setContractDemand("b");


            return bidReport;
        }).collect(Collectors.toList());
        returnBidCommonPage.setList(collect);

        Long pages = page.getPages();
        Integer total = Integer.valueOf(pages.toString());
        returnBidCommonPage.setTotalPage(total);
        returnBidCommonPage.setPageSize(pageSize);

        returnBidCommonPage.setPageNum(pageNum);
        returnBidCommonPage.setTotal(page.getTotal());
        return returnBidCommonPage;
    }


}
